import http from '../../apis/api.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeNo:'',
    prodNo:'',
    custNo:'',/**客户编码 */
    storeDtl:null,//民宿详情
    vouchers: [],/**代金券列表 */
    selectVoucher:{},/**选中优惠券 */
    showVouchers:false,
    buyNum:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self=this;
    self.data.storeNo = options.storeNo;
    self.data.prodNo = options.prodNo;
    self.data.buyNum = options.buyNum ? options.buyNum:1;
    wx.getStorage({
      key: 'loginInfo',
      success: function (res) {
        if (res.data.id) {
          self.data.custNo = res.data.custNo;
        }
      }    
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    http.storeDtl({
      storeNo: self.data.storeNo
    }).then(res=>{
      res.data.thumbnail ='';
      res.data.storePics.forEach(el => {
        if (el.picType == "51000101") {
          res.data.thumbnail=el.inPath;
        }
      })
      self.data.storeDtl = res.data;
      self.setData({
        storeDtl: self.data.storeDtl
      })
    })
    // 获取民宿代金券
    http.getProdByStoreNo({ storeNo: self.data.storeNo}).then(res => {
      self.data.vouchers = res.data;
      console.log(self.data.vouchers);
      self.data.vouchers.forEach(el=>{
        if(el.prodNo===self.data.prodNo){
          self.data.selectVoucher=el;
          self.data.selectVoucher.formatProdEndTime =util.formatTime(el.prodEndTime,'YYYY年MM月DD日')
          self.data.selectVoucher.buyNum = self.data.buyNum;
          self.data.selectVoucher.maxBuy = Number(self.data.selectVoucher.totalNum) - Number(self.data.selectVoucher.saleNum) > self.data.selectVoucher.maxBuyNum ? self.data.selectVoucher.maxBuyNum : Number(self.data.selectVoucher.totalNum) - Number(self.data.selectVoucher.saleNum) ;
          self.data.selectVoucher.totalPrice = (Number(self.data.selectVoucher.buyNum) * Number(self.data.selectVoucher.saleAmt)).toFixed(2)
        }
      })
      self.setData({
        vouchers: self.data.vouchers,
        selectVoucher: self.data.selectVoucher
      })
      console.log(self.data.selectVoucher);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeNum(e){
    let type = e.target.dataset.type;
    switch(type){
      case 'add':
        self.data.selectVoucher.buyNum < self.data.selectVoucher.maxBuy ? self.data.selectVoucher.buyNum++ : self.data.selectVoucher.maxBuy;
      break;
      case 'jian':
        self.data.selectVoucher.buyNum>1?self.data.selectVoucher.buyNum--:'';
        break;
      case 'input':
        self.data.selectVoucher.buyNum = e.type === 'input' ? e.detail.value : e.detail.value > 1 ? (e.detail.value < self.data.selectVoucher.maxBuy ? e.detail.value : self.data.selectVoucher.maxBuy ):1;
        break;
    }
    self.data.selectVoucher.totalPrice = (Number(self.data.selectVoucher.buyNum) * Number(self.data.selectVoucher.saleAmt)).toFixed(2)
    self.setData({
      "selectVoucher": self.data.selectVoucher
    })
  },
  orderNow(e){/**提交订单 */
    wx.showLoading()
    let postData = {
      buyNum: self.data.selectVoucher.buyNum,
      custNo: self.data.custNo,
      openId: '',
      prodNo: self.data.selectVoucher.prodNo
    };
    wx.getStorage({ key: 'openid',success(result){
      postData.openId = result.data;
      http.buyProd(postData).then(buy=>{
        wx.hideLoading();
        if (buy.code==0){
          /**代金券购买成功 */
          wx.showModal({
            title: '提交订单成功',
            content: '是否立即支付',
            success: function (res) {
              if (res.confirm) {
                let wxParams={
                  timeStamp: buy.data.weChatAppletOrde.timeStamp,
                  nonceStr: buy.data.weChatAppletOrde.nonceStr,
                  package: buy.data.weChatAppletOrde.pay_package,
                  signType: buy.data.weChatAppletOrde.signType,
                  paySign: buy.data.weChatAppletOrde.paySign,
                }
                util.wxPay(wxParams).then(res=>{
                  wx.redirectTo({
                    url: "/pages/orderList/orderList",
                  })
                },error=>{
                  wx.redirectTo({
                    url: "/pages/orderList/orderList",
                  })
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: "/pages/orderList/orderList",
                })
              }
            }
          })
         
        }else{
          wx.showToast({
            title: buy.msg,
            icon: 'none',
            duration: 3000
          })
        }
      })
    }});
  },
  changeShow(){
    self.data.showVouchers = !self.data.showVouchers;
    self.setData({
      showVouchers: self.data.showVouchers
    })
  },
  radioChange(e){
    let value = e.detail.value;
    self.data.vouchers.forEach(el => {
      if (el.id == value) {
        self.data.selectVoucher = el;
        self.data.selectVoucher.formatProdEndTime = util.formatTime(el.prodEndTime, 'YYYY年MM月DD日')
        self.data.selectVoucher.buyNum = self.data.buyNum;
        self.data.selectVoucher.maxBuy = Number(self.data.selectVoucher.totalNum) - Number(self.data.selectVoucher.saleNum);
        self.data.selectVoucher.totalPrice = (Number(self.data.selectVoucher.buyNum) * Number(self.data.selectVoucher.saleAmt)).toFixed(2)
      }
    })
    self.setData({
      selectVoucher: self.data.selectVoucher
    })
  },
  goShouce() {
    wx.navigateTo({
      url: `/pages/agreement/agreement?url=${encodeURIComponent(' https://ms.maishe.live/tenantAgreement.html')}&type='user'`
    })
  }
})